<?php 
/***************************************************************************
Plugin Name:  Dashboard Widgets
Plugin URI:   http://mektoub.free.fr
Description:  Displays recent ratings on your WordPress 2.7+ dashboard. Based on WP-PostRatings plugin.
Version:      1.0
Author:       Manuel EVENO
**************************************************************************/

// Load up the localization file if we're using WordPress in a different language
// Place it in this plugin's folder and name it "dashboard_last_ratings-[value in wp-config].mo"
load_plugin_textdomain( 'dashboard_last_ratings', '/wp-content/plugins/dashboard_last_ratings' );

function dashboard_top_pages() {
	if (function_exists('iriValueTable')) {
		# Top Pages
		iriValueTable("urlrequested",'',5,"","urlrequested","AND feed='' and spider='' AND urlrequested LIKE '%p=%' ");
	}
}

function dashboard_last_hits() {
	global $wpdb;
	$querylimit = "LIMIT 5";
	$table_name = "blog_statpress";

	# Tabella Last hits
	print "<div class='wrap'><table class='widefat'><thead><tr><th scope='col'>". __('Date','statpress'). "</th><th scope='col'>". __('Time','statpress'). "</th><th scope='col'>". __('Hostname','statpress'). "</th><th scope='col'>". __('Page','statpress'). "</th></tr></thead>";
	print "<tbody id='the-list'>";	

	$fivesdrafts = $wpdb->get_results("SELECT * FROM $table_name WHERE (os<>'' OR feed<>'') order by id DESC $querylimit");
	foreach ($fivesdrafts as $fivesdraft) {
		print "<tr>";
		//$date = mysql2date(get_option('date_format'), substr($dt,0,4)."-".substr($dt,4,2)."-".substr($dt,6,2));
		$dt = $fivesdraft->date;
		$date = substr($dt,6,2).'/'.substr($dt,4,2).'/'.substr($dt,0,4);
		//$date = irihdate($fivesdraft->date);
		print "<td>". $date ."</td>";
		print "<td>". $fivesdraft->time ."</td>";
		if ($fivesdraft->hostname) {
			print "<td>". $fivesdraft->hostname ."</td>";
		} else {
			print "<td>". gethostbyaddr($fivesdraft->ip) ."</td>";
		}
		//print "<td>". $fivesdraft->nation ."</td>";
		print "<td>". iri_StatPress_Abbrevia(iri_StatPress_Decode($fivesdraft->urlrequested),30) ."</td>";
		print "</tr>";
	}
	print "</table></div>";
}

function dashboard_last_ratings_show() {

	// Add the widget to the dashboard

	global $wpdb;

	$widget_options = get_option('dashboard_last_ratings');

	//echo '<p>Debug: method: '.$_SERVER['REQUEST_METHOD'].', id: '.$_POST['widget_id'].'</p>';
	if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'dashboard_last_ratings' == $_POST['widget_id'] ) {
		echo "<p>Updating options ...</p>";
		foreach ( array( 'widget_title', 'item_count', 'showtime', 'showauthor' ) as $key ) {
			$widget_options[$key] = $_POST[$key];
		}
		update_option( 'dashboard_last_ratings', $widget_options) ;
	}

	$request  = "SELECT `rating_rating` as rating, `rating_posttitle` as title, `rating_timestamp` as date, `rating_username` as author";
	$request .= " FROM $wpdb->ratings ORDER BY `rating_timestamp` DESC LIMIT 0, ".$widget_options['item_count'];
	$ratings = $wpdb->get_results($request);	

	if ( $ratings ) {
		echo '<table id="dashboard_last_ratings" width="100%"><tbody>';
		$ratings_image = get_option('postratings_image');
		$ratings_max = intval(get_option('postratings_max'));
		foreach ( $ratings as $rate ) {
			$rate_image = '';
			for ($i = 1; $i <= $rate->rating; $i++) {
				$rate_image .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_on.'.RATINGS_IMG_EXT).'" />';
			}
			for ($i = $rate->rating+1; $i <= $ratings_max; $i++) {
				$rate_image .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_off.'.RATINGS_IMG_EXT).'" />';
			}
			//$rate_date = mysql2date( get_option('date_format') . ' @ ' . get_option('time_format'), gmdate('Y-m-d H:i:s', $rate->date));
			$rate_date = gmdate('d/m/Y', $rate->date);// H:i
			echo '<tr>';
			echo '<td><span style="font-size: 8pt">' . $rate_image . '</span></td>';
			echo '<td><span style="font-size: 8pt">' . $rate->title . '</span></td>';
			if ($widget_options['showauthor']) {
				echo '<td><span style="font-size: 8pt">' . $rate->author . '</span></td>';
			}
			if ($widget_options['showtime']) {				
				echo '<td><span style="font-size: 8pt">' . $rate_date . '</span></td>';
			}
			echo '</tr>';
		}

		echo '</tbody></table>';

	} else {
		echo '<p>' . __( "Sorry! You don't have any ratings yet in your database!", 'dashboard_last_ratings' ) . "</p>\n";
	}

}

function dashboard_last_ratings_configure() {
	$options = get_option('dashboard_last_ratings');
?>
   <p>
		<label for="widget_title"><?php _e('Modify widget title: ', 'dashboard_last_ratings' ); ?>
			<input id="widget_title" name="widget_title" type="text" value="<?php echo $options['widget_title']; ?>" />
		</label>
	</p>

	<p>
		<label for="items"><?php _e('How many recent ratings would you like to display?', 'dashboard_last_ratings' ); ?>
			<select id="items" name="items">
				<?php
					for ( $i = 5; $i <= 20; $i = $i + 1 )
						echo "<option value='$i'" . ( $options['item_count'] == $i ? " selected='selected'" : '' ) . ">$i</option>";
				?>
			</select>
		</label>
	</p>

   <p>
		<label for="showauthor">
			<input id="showauthor" name="showauthor" type="checkbox" value="1"<?php if ( 1 == $options['showauthor'] ) echo ' checked="checked"'; ?> />
			<?php _e('Show author?', 'dashboard_last_ratings' ); ?>
		</label>
	</p>
	
   <p>
		<label for="showtime">
			<input id="showtime" name="showtime" type="checkbox" value="1"<?php if ( 1 == $options['showtime'] ) echo ' checked="checked"'; ?> />
			<?php _e('Show date?', 'dashboard_last_ratings' ); ?>
		</label>
	</p>
	
<?php
	}

function dashboard_last_ratings_update() {
	$options= get_option( 'dashboard_last_ratings') ;
	wp_add_dashboard_widget(
		'dashboard_last_ratings',
		$options['widget_title'],
		'dashboard_last_ratings_show',
		'dashboard_last_ratings_configure'
	);
	$options= get_option( 'dashboard_top_pages') ;
	wp_add_dashboard_widget(
		'dashboard_top_pages',
		$options['widget_title'],
		'dashboard_top_pages'
	);
	$options= get_option( 'dashboard_last_hits') ;
	wp_add_dashboard_widget(
		'dashboard_last_hits',
		$options['widget_title'],
		'dashboard_last_hits'
	);
}

function dashboard_last_ratings_init() {
	$defaults = array( 'widget_title' => 'Derni&egrave;res notes', 'item_count' => 5, 'showtime' => 1, 'showauthor' => 1);
	update_option( 'dashboard_last_ratings', $defaults) ;

	$defaults = array( 'widget_title' => 'Top pages', 'item_count' => 5);
	update_option( 'dashboard_top_pages', $defaults) ;

	$defaults = array( 'widget_title' => 'Last hits', 'item_count' => 5);
	update_option( 'dashboard_last_hits', $defaults) ;
}

/**
 * use hook, to integrate new widget
 */
add_action("plugins_loaded", 'dashboard_last_ratings_init');
add_action('wp_dashboard_setup', 'dashboard_last_ratings_update');

?>
